﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using WindowsForms.service;
using WindowsForms.Class;

namespace WindowsForms
{
    public partial class TableProcess : DevExpress.XtraEditors.XtraForm
    {
        Service1 iQuery;
        List<Regions> lsRegion;
       
        public TableProcess(bool? isModifyer = null)
        {
            iQuery = new Service1();
            InitializeComponent();
            GetRegions();
            if (isModifyer.HasValue)
            {
                GenerateModifyerControl(isModifyer.Value);
            }
        }

        private void GetRegions()
        {
            ConvertToList convert = new ConvertToList();
            lsRegion = convert.ConvertTolistRegions(iQuery.SelectRegionsAll().Tables[0]);
            cbRegion.Properties.DataSource = lsRegion;
        }

        private void GenerateModifyerControl(bool isModifyer)
        {
            if (isModifyer)
            {
                this.layoutControlGroupTable.Text = "Chỉnh sửa thông tin bàn";
                this.layoutControlGroupTable.CustomizationFormText = "Chỉnh sửa thông tin bàn";
                this.layoutControlGroupTable.CaptionImage = global::WindowsForms.Properties.Resources.Edit;
            }
        }
        #region get, set
        public string Table_Name
        {
            get
            {
                return txtTableName.Text;
            }
            set
            {
                txtTableName.Text = value;
            }
        }
        public string Table_Des
        {
            get
            {
                return txtDes.Text;
            }
            set
            {
                txtDes.Text = value;
            }
        }
        public int Table_RegionID
        {
            get
            {
                return (int)cbRegion.EditValue;
            }
            set
            {
                cbRegion.EditValue = value;
            }
        }
        public Regions Region_Edit
        {
            get
            {
                return lsRegion.Find(delegate(Regions id)
                {
                    return id.RegionID == (int)cbRegion.EditValue;
                });
            }
        }
        #endregion

        private void TableProcess_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !dxValidationTable.Validate() && DialogResult == DialogResult.OK;
        }

        private void btnRegion_Click(object sender, EventArgs e)
        {
            using (RegionProcess frmRegion = new RegionProcess())
            {
                if (frmRegion.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    Regions regionObj = new Regions();
                    regionObj.RegionName = frmRegion.Region_Name;
                    regionObj.Description = frmRegion.Region_Des;
                    iQuery.InsertRegion(regionObj.RegionName, regionObj.Description);
                    lsRegion.Add(regionObj);
                    GetRegions();
                }
            }
        }

        private void TableProcess_Load(object sender, EventArgs e)
        {

        }
    }
}